package com.union.core.sys.dao.impl;
/**
 * 接口实现: 角色
 * filename: RoleDaoImpl.java
 * project:  union
 * @company Hainan Jowe Technology Ltd
 * @author Harry Zhang
 * @create in 2012-10-29
 * @version 1.0
 */
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.hnjowe.webdb.DBOperation;
import com.union.core.sys.dao.RoleDao;
import com.union.core.sys.model.Role;

public class RoleDaoImpl implements RoleDao {
	DBOperation opera;
	private static Logger log = Logger.getLogger(RoleDaoImpl.class.getName());
	public boolean addRole(Role role) {
		boolean bl=false;
		try{
			opera=new DBOperation();
			opera.transcationBegin();
			String sql="insert into sys_role (role_name,add_manager,add_time,role_type,role_org) values(?,?,SYSDATE(),?,?)";
			opera.prepStatment(sql);
			opera.setString(1, role.getRole_name());
			opera.setString(2, role.getAdd_manager());
			opera.setInt(3, role.getRole_type());
			opera.setInt(4, role.getOrg_id());
			bl=opera.execute();
			opera.transcationCommit();
		}catch(Exception e){
			log.error(e);
		}finally{
			opera.closePres();
			opera.closeConn();
		}
		return bl;
	}

	public boolean delRole(int roleId) {
		boolean bl=false;
		try{
			opera=new DBOperation();
			opera.transcationBegin();
			String sql="delete from sys_role where id=?";
			opera.prepStatment(sql);
			opera.setInt(1, roleId);
			bl=opera.execute();
			opera.transcationCommit();
		}catch(Exception e){
			log.error(e);
		}finally{
			opera.closePres();
			opera.closeConn();
		}
		return bl;
		
	}

	public Role queryByid(int roleId) {
		opera=new DBOperation();
		Role role=new Role();
		try{
		opera.transcationBegin();
		String sql="select id,role_name,add_manager,add_time,role_weight,role_permissions from sys_role where id="+roleId;
		opera.prepStatment(sql);
		ResultSet rs=opera.executeQuery();
		
		if(rs.next()){
			
			role.setId(rs.getInt(1));
			role.setRole_name(rs.getString(2));
			role.setAdd_manager(rs.getString(3));
			role.setAdd_time(rs.getString(4));
			role.setRole_weight(rs.getInt(5));
			role.setRole_permissions(rs.getString(6));
		}
		
		}catch(Exception e){
			log.error(e);
		}finally{
			opera.closePres();
			opera.closeConn();
		}
		return role;
	}

	public List<Role> queryRoleList(int roleId, String roleName,
			String addManager, String addTime, String parName,int roleType,int org_id, String parVal,
			String orderBy, int start, int rownum) {
		opera=new DBOperation();
		List<Role> roleList=new ArrayList<Role>();
		String sqlstr1="";
		String sqlstr2="";
		String sqlstr3="";
		String sqlstr4="";
		String sqlstr5="";
		String sqlstr6="";
		String sqlstr7="";
		String sqlstr8="";
		String sqlstr9="";
		String orderbySql = ""; //排序条件
		
		if (roleId!=0) sqlstr1 = " and id="+roleId;
		if (roleName!=null) sqlstr2 = " and role_name='"+roleName+"'";
		if (addManager!=null) sqlstr3 = " and add_manager='"+addManager+"'";
		
		
		if (parName != null && parVal != null) {
			if(parName.equals("role_name")) sqlstr7 = " and role_name like '%"+parVal+"%'";
			if(parName.equals("add_manager")) sqlstr7 = " and add_manager like '%"+parVal+"%'";
		}
		if(roleType==0 && org_id!=0)sqlstr4=" and(role_type=0 or role_org="+org_id+")";
		if(roleType==1 && org_id!=0)sqlstr4=" and role_org="+org_id;
		if(roleType==2)sqlstr4=" and role_type=0";
		if (orderBy != null&& !"".equals(orderBy)) {
			String str[] = orderBy.split("-");			
			if (str[1].equals("UP"))
				orderbySql = " order by " + str[0];
			if (str[1].equals("DOWN"))
				orderbySql = " order by " + str[0] + " desc";
		} else {
			orderbySql = " order by id asc";
		}
		try{
		opera.transcationBegin();
		String sql="select id,role_name,add_manager,add_time,role_weight,role_permissions from sys_role where id>0"
			+sqlstr1+sqlstr2+sqlstr3+sqlstr4+sqlstr5+sqlstr6+sqlstr7+sqlstr8+sqlstr9+orderbySql+" limit ?,?";
		opera.prepStatment(sql);
		opera.setInt(1, start);
		opera.setInt(2, rownum);
		ResultSet rs=opera.executeQuery();
		
		while(rs.next()){
			Role role=new Role();
			role.setId(rs.getInt(1));
			role.setRole_name(rs.getString(2));
			role.setAdd_manager(rs.getString(3));
			role.setAdd_time(rs.getString(4));
			role.setRole_weight(rs.getInt(5));
			role.setRole_permissions(rs.getString(6));
			roleList.add(role);
		}
		
		}catch(Exception e){
			log.error(e);
		}finally{
			opera.closePres();
			opera.closeConn();
		}
		return roleList;
	}
	
	public int queryRoleListCount(int roleId, String roleName,
			String addManager, String addTime, String parName,int roleType,int org_id, String parVal) {
		int count=0;
		opera=new DBOperation();
		String sqlstr1="";
		String sqlstr2="";
		String sqlstr3="";
		String sqlstr4="";
		String sqlstr5="";
		String sqlstr6="";
		String sqlstr7="";
		String sqlstr8="";
		String sqlstr9="";
		try{
		if (roleId!=0) sqlstr1 = " and id="+roleId;
		if (roleName!=null) sqlstr2 = " and role_name='"+roleName+"'";
		if (addManager!=null) sqlstr3 = " and add_manager='"+addManager+"'";
		
		
		if (parName != null && parVal != null) {
			if(parName.equals("role_name")) sqlstr7 = " and role_name like '%"+parVal+"%'";
			if(parName.equals("add_manager")) sqlstr7 = " and add_manager like '%"+parVal+"%'";
		}
		opera.transcationBegin();
		if(roleType==0 && org_id!=0)sqlstr4=" and(role_type=0 or role_org="+org_id+")";
		if(roleType==1 && org_id!=0)sqlstr4=" and role_org="+org_id;
		if(roleType==2)sqlstr4=" and role_type=0";
		String sql="select count(*) from sys_role where id>0"
			+sqlstr1+sqlstr2+sqlstr3+sqlstr4+sqlstr5+sqlstr6+sqlstr7+sqlstr8+sqlstr9;
		opera.prepStatment(sql);

		ResultSet rs=opera.executeQuery();
		if(rs.next()){
			count=rs.getInt(1);
		}
		
		}catch(Exception e){
			log.error(e);
		}finally{
			opera.closePres();
			opera.closeConn();
		}
		return count;
	}

	public boolean update(Role role) {
		boolean bl=false;
		try{
			opera=new DBOperation();
			opera.transcationBegin();
			String sql="update sys_role set role_name=?,role_permissions=?,role_weight=? where id=?";
			opera.prepStatment(sql);
			opera.setString(1, role.getRole_name());
			opera.setString(2, role.getRole_permissions());
			opera.setInt(3, role.getRole_weight());
			opera.setInt(4, role.getId());
			opera.executeUpdate();
			bl=opera.transcationCommit();
		}catch(Exception e){
			log.error(e);
		}finally{
			opera.closePres();
			opera.closeConn();
		}
		return bl;
	}
	public boolean checkRoleName(String roleName){
		boolean result = false;
	    try{
	      try{
	        opera = new DBOperation();
	        opera.transcationBegin();
	        String sql = "select * "
	                   + "from sys_role "
	                   + "where role_name=?";
	        opera.prepStatment(sql);
	        opera.setString(1,roleName);
	        ResultSet res = opera.executeQuery();
	        if(res.next()){
	          result = true;
	        }
	      }catch(Exception ex){
	        result = false;
	      }
	    }catch(Exception e){
	      e.printStackTrace();
	    }finally{
	      opera.closePres();
	      opera.closeConn();
	    }
	    return result;
	}
}
